package com.derongonline.fragment;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SwitchCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;

import com.derongonline.R;
import com.derongonline.activity.GuideActivity;
import com.derongonline.activity.HomeActivity;
import com.derongonline.activity.MainActivity;
import com.derongonline.adapter.DemoAdapter;

import java.util.ArrayList;

/**
 *
 */
public class DemoFragment extends Fragment {

    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;

    /**
     * Create a new instance of the fragment
     */
    public static DemoFragment newInstance(int index) {
        DemoFragment fragment = new DemoFragment();
        Bundle b = new Bundle();
        b.putInt("index", index);
        fragment.setArguments(b);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (getArguments().getInt("index", -1) == 0) {
            final HomeActivity homeActivity = (HomeActivity) f
            View view = inflater.inflate(R.layout.activity_home, container, false);
            //initDemoSettings(view);
            final MainActivity demoActivity = (MainActivity) getActivity();

            final Button button = (Button) view.findViewById(R.id.toDetail);
            button.setOnClickListener(new CompoundButton.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(demoActivity, GuideActivity.class);
                    startActivity(intent);
                    demoActivity.finish();
                    return;
                }
            });
            return view;
        } else if(getArguments().getInt("index", -1) == 1) {
            View view = inflater.inflate(R.layout.activity_loan, container, false);
            //initDemoList(view);
            return view;
        }else{
            View view = inflater.inflate(R.layout.activity_user, container, false);
            //initDemoList(view);
            return view;
        }
    }

    /**
     * Init demo settings
     */
    private void initDemoSettings(View view) {
        final MainActivity demoActivity = (MainActivity) getActivity();
        final SwitchCompat switchColored = (SwitchCompat) view.findViewById(R.id.fragment_demo_switch_colored);
        final SwitchCompat switchFiveItems = (SwitchCompat) view.findViewById(R.id.fragment_demo_switch_five_items);
        final Button button = (Button) view.findViewById(R.id.toDetail);
        button.setOnClickListener(new CompoundButton.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("11111","2222");
            }
        });
        switchColored.setChecked(demoActivity.isBottomNavigationColored());
        switchFiveItems.setChecked(demoActivity.getBottomNavigationNbItems() == 5);

        switchColored.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                demoActivity.updateBottomNavigationColor(isChecked);
            }
        });
        switchFiveItems.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                demoActivity.updateBottomNavigationItems(isChecked);
            }
        });
    }

    /**
     * Init the fragment
     */
    private void initDemoList(View view) {

        recyclerView = (RecyclerView) view.findViewById(R.id.fragment_demo_recycler_view);
        recyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);

        ArrayList<String> itemsData = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            itemsData.add("Fragment " + getArguments().getInt("index", -1) + " / Item " + i);
        }

        DemoAdapter adapter = new DemoAdapter(itemsData);
        recyclerView.setAdapter(adapter);
    }

    /**
     * Refresh
     */
    public void refresh() {
        recyclerView.smoothScrollToPosition(0);
    }
}
